package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SpBp1Mapper;
import com.ruoyi.system.domain.SpBp1;
import com.ruoyi.system.service.ISpBp1Service;


import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.mapper.DzHuiyuanMapper;
import com.ruoyi.system.domain.DzHuiyuan;
import com.ruoyi.system.service.IDzHuiyuanService;

/**
 * 编排得分Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-11-12
 */
@Service
public class SpBp1ServiceImpl implements ISpBp1Service 
{
    @Autowired
    private SpBp1Mapper spBp1Mapper;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private IDzHuiyuanService dzHuiyuanService;

    /**
     * 查询编排得分
     * 
     * @param cj1Id 编排得分主键
     * @return 编排得分
     */
    @Override
    public SpBp1 selectSpBp1ByCj1Id(Long cj1Id)
    {
        return spBp1Mapper.selectSpBp1ByCj1Id(cj1Id);
    }

    /**
     * 查询编排得分列表
     * 
     * @param spBp1 编排得分
     * @return 编排得分
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<SpBp1> selectSpBp1List(SpBp1 spBp1)
    {

        //权限控制
        SysUser user=  SecurityUtils.getLoginUser().getUser();
        List<SysRole> listRole= sysRoleService.selectRolesByUserId(user.getUserId());
        int datascope = 5;
        for(SysRole role:listRole){
            if( role.getDataScope()!=null&&Integer.parseInt(role.getDataScope())<datascope){
                datascope=Integer.parseInt(role.getDataScope());
            }
        }
        spBp1.setDatascope(datascope);
        if(datascope==4){
            spBp1.setCheckDeptId(user.getDeptId());
        }
        if(datascope==5){
            DzHuiyuan huiyuan =  dzHuiyuanService.selectDzHuiyuanByDn(user.getPhonenumber());
            if(huiyuan!=null){
                spBp1.setCheckHuiyuanId(huiyuan.getId());
            }else{
                spBp1.setCheckHuiyuanId(0l);
            }

        }
        return spBp1Mapper.selectSpBp1List(spBp1);
    }

    @Override
    public List<SpBp1> selectSpBp1ListWeb(SpBp1 spBp1)
    {
        return spBp1Mapper.selectSpBp1List(spBp1);
    }

    /**
     * 新增编排得分
     * 
     * @param spBp1 编排得分
     * @return 结果
     */
    @Override
    public int insertSpBp1(SpBp1 spBp1)
    {
        spBp1.setCreateTime(DateUtils.getNowDate());
        return spBp1Mapper.insertSpBp1(spBp1);
    }

    /**
     * 修改编排得分
     * 
     * @param spBp1 编排得分
     * @return 结果
     */
    @Override
    public int updateSpBp1(SpBp1 spBp1)
    {
        return spBp1Mapper.updateSpBp1(spBp1);
    }

    /**
     * 批量删除编排得分
     * 
     * @param cj1Ids 需要删除的编排得分主键
     * @return 结果
     */
    @Override
    public int deleteSpBp1ByCj1Ids(Long[] cj1Ids)
    {
        return spBp1Mapper.deleteSpBp1ByCj1Ids(cj1Ids);
    }

    /**
     * 删除编排得分信息
     * 
     * @param cj1Id 编排得分主键
     * @return 结果
     */
    @Override
    public int deleteSpBp1ByCj1Id(Long cj1Id)
    {
        return spBp1Mapper.deleteSpBp1ByCj1Id(cj1Id);
    }
}
